
const chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

const random = function generateMixed(n) {
  var res = "";
  for (var i = 0; i < n; i++) {
    var id = Math.ceil(Math.random() * 35);
    res += chars[id];
  }
  return res;
}

const promisic = function(func){
    return function(params={}){
        return new Promise((resolve, reject)=>{
            const args = Object.assign(params, {
                success:(res)=>{
                    // console.log(res)
                    // console.log(2222)
                    resolve(res)
                },
                fail:(error)=>{
                    reject(error)
                }
            })
            func(args)
        })
    }
}

const getTime = function (start) {
    let nowTime = new Date()
    let oldTime = new Date(start)
    let diff = Math.floor((nowTime - oldTime) / 1000 / 60)
    let time = start
    if (diff <= 59) {
        time = diff + '分钟前'
    } else if (diff <= 60 * 24 - 1) {
        time = Math.floor(diff / 60) + '小时前'
    } else if (diff <= 60 * 24 * 3 - 1) {
        time = Math.floor(diff / 60 / 24) + '天前'
    } else {
        // time = time.split(" ")[0]   // startTime格式：yyyy-mm-dd hh:ii:ss
        time = getNowFormatDate(time)
    }
    return time
}

const getNowFormatDate = function(time) {
    let date = new Date(time);
    let seperator1 = "-";
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    return year + seperator1 + month + seperator1 + strDate;
}

export {random, promisic, getTime}
